﻿#ifndef QQMAIN_H
#define QQMAIN_H

#include <QSystemTrayIcon>

#include "direction.h"
#include "httpclient.h"
#include "fileoperator.h"
#include "info_item.h"
#include "frienditem.h"
#include "addrelation_menu.h"
#include "websocketclient.h"
#include "searchfriend.h"
#include "orientation.h"

#include <QScreen>
#include <QPoint>
#include <QDesktopWidget>
#include <QMouseEvent>

#include <QGraphicsDropShadowEffect>
#include <QGraphicsView>
#include <QGraphicsScene>

#include <QSqlDatabase>
#include <QSqlError>

#include <QJsonDocument>
#include <QJsonObject>
#include <QDateTime>
#include <QMenu>

namespace Ui {
class QQMain;
}

#define MARGIN_MIN_SIZE 2
#define MARGIN_MAX_SIZE 6

class QQMain : public QWidget
{
    Q_OBJECT

public:
    explicit QQMain(HttpClient *httpClient,WebSocketClient* webSocketClient,FileOperator* fileOperator,SQLiteUtil* sQLiteUtil,const QString &email,const QString &username,const bool &isDownLoad,const QString &sign,QWidget *parent = nullptr);
    ~QQMain();
signals:
    void quit();
    void showComm(const QString &email,const QString &name,const QString &message);
    void resizeWidth(int width);
    void updateApplication(const QString &email);
    void updateFriendList(const QString &email,const QString &username);
private slots:
    void infoItemClick(Info_item* newItem);
    void friendItemClick(FriendItem* newItem);
    void getRecentMessage(const QString &email,const QString &name);
    void updateFriend(const QString &email,const QString &content,const QString &createTime);
protected:
    bool event(QEvent *event);
    void leaveEvent(QEvent *event);
    void mousePressEvent(QMouseEvent *event);
    void mouseMoveEvent(QMouseEvent *event);
    void mouseReleaseEvent(QMouseEvent *event);
    void resizeEvent(QResizeEvent *event);
private:
    Ui::QQMain *ui;
    AddRelation_menu *relationMenu=nullptr;
    HttpClient *httpClient=nullptr;
    WebSocketClient* webSocketClient=nullptr;
    FileOperator* fileOperator=nullptr;
    SQLiteUtil* sQLiteUtil=nullptr;
    SearchFriend* searchFriend=nullptr;

    bool m_bIsPressed=false;//是否按下鼠标
    bool m_bIsResizing=false;//是否正在调整窗口，调整窗口大小时不移动窗口
    QPoint m_movePoint;//记录鼠标的位置
    QPoint m_pressPoint;//记录鼠标按住的位置
    Direction m_direction=Direction::NONE;

    void updateRegion(QMouseEvent *event);
    void resizeRegion();
    void extendLeft(QRect &rect);
    void extendRight(QRect &rect);
    void extendTop(QRect &rect);
    void extendDown(QRect &rect);
    void createShadow();

    QString headImgUrl="/App/img?name=get_head_img";
    QString email;

    void loadHttpComponentRespond();
    void loadSocketComponentRespond();
    void loadBaseComponentRespond();
    void loadSearchFriendRespond();

    void updateHeadImg();
    void setFriendList();
     void setInfoPage();
    void hideMenu();
    void updateInfoPage(QVariantMap &map,const QString &msger);
    Info_item* createInfoItem(const QString &email,const QString &username,QVariantMap &msgMap);
    QString getMsgForType(QVariantMap &map);
    QString getUserName(QVariantMap &map);
    void showSearchFriendWidget();
    void updateFriendsHImg(QString &email);
    void updateInfoListHImg(QString &email);
    void updateFriendInfo(QVariantMap &map);
    void saveMessage(QVariantMap &map);
    void insertFriendList(const QString &email,QVariantMap &map,const OrientType &type);
    void insertInfoList(const QString &email,const QString &username,QVariantMap &map,const OrientType &type);
};

#endif // QQMAIN_H
